package com.bawei.module_home.ui.house


import android.util.Log
import android.view.View
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.adapter.HouseAdapter
import com.bawei.module_home.databinding.ActivityHouseBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.HouseItem
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = Router.HOUSE_PAGE)
class HouseActivity :BaseActivity<BaseViewModel,ActivityHouseBinding>() {
    override val mLayoutResId: Int
        get() = com.bawei.module_home.R.layout.activity_house

    //适配器
     val mAdapter:HouseAdapter by lazy {
         HouseAdapter()
    }

    //viewModel
    val mViewModel: HomeServiceViewModel by viewModels()

    override fun initView() {
        super.initView()
        mBinding.houseRv.adapter=mAdapter
        mBinding.houseRv.layoutManager= LinearLayoutManager(this)




    }

    fun showPopupMenu( view: View){
        val popupMenu: PopupMenu = PopupMenu(this,view )
        popupMenu.getMenuInflater().inflate(com.bawei.module_home.R.menu.popup_menu_items, popupMenu.getMenu())
        popupMenu.setOnMenuItemClickListener { item ->
            when (item.itemId) {
                com.bawei.module_home.R.id.menu_item_1 -> {
                    ARouter.getInstance().build(Router.HOUSE_MANAGE_PAGE).navigation()
                }
                com.bawei.module_home.R.id.menu_item_2 -> {
                    ToastUtils.showLong(this, "暂时没有数据")
                }
            }
            true
        }
        popupMenu.show()

    }

    override fun initListener() {
        mBinding.back.setOnClickListener {
            finish()
        }

        mBinding.more.setOnClickListener {
          showPopupMenu( it)
        }




        mViewModel.houseState2.observeWithLifecycle(this){
            when(it){
                Empty -> {

                }
                is Failed -> {
                    ToastUtils.showLong(this, it.message)
                }
                Loading -> {

                }
                Success -> {
                    Log.d("fcc1", "initListener: "+mViewModel.houseList2.size)
                    mAdapter.mAdapter2.setData(mViewModel.houseList2)
                }
            }
        }


        mViewModel.houseState.observeWithLifecycle(this){
            when(it){
                Empty -> {

                }
                is Failed -> {
                    ToastUtils.showLong(this, it.message)
                }
                Loading -> {

                }
                Success -> {
                   mAdapter.setData(mViewModel.houseList)
                }
            }
        }

        //点击事件
        mAdapter.itemClickListener=object : HouseAdapter.OnItemClickListener{
            override fun onItemClick(item: HouseItem, position: Int) {
                Log.d("fcc2", "initListener: "+item.regionId.toString())
                mViewModel.getHouse2(item.regionId)

            }
        }
    }

    override fun initData() {
        mViewModel.getAllHouse()

    }



}